package com.luoguijia.disk.user.service.impl;

import com.luoguijia.disk.mybatis.entity.PtUserEntity;
import com.luoguijia.disk.mybatis.flexigrid.PagingEntity;
import com.luoguijia.disk.mybatis.flexigrid.TblResponseEntity;
import com.luoguijia.disk.mybatis.ztree.ZtreeEntity;
import com.luoguijia.disk.user.entity.SearchUserEntity;
import com.luoguijia.disk.user.exeption.EmailRepeatException;
import com.luoguijia.disk.user.exeption.MobileRepeatException;
import com.luoguijia.disk.user.exeption.UserNameRepeatException;
import com.luoguijia.disk.user.exeption.UserOccupyException;
import com.luoguijia.disk.user.repository.UserDao;
import com.luoguijia.disk.user.service.UserService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;


    @Resource
    private PasswordEncoder passwordEncoder;

    @Override
    public List<PtUserEntity> queryUserList() {
        return userDao.queryUserList();
    }

    @Override
    public void deleteUserByIds(String ids) throws UserOccupyException {
        String[] idArray = ids.split(",");
        String niceName = null;
        StringBuffer sb = new StringBuffer();
        for(String id:idArray){
            niceName = userDao.queryRoleListByUser(id);
            if(niceName != null){
                sb.append(niceName);
                sb.append(",");
            }
        }
        String rstStr= sb.toString();
        if(!"".equals(rstStr)){
            rstStr = rstStr.substring(0,rstStr.length() - 1);
            throw  new UserOccupyException(rstStr);
        }
        for (String id:idArray){
            userDao.deleteUserById(id);
        }
    }

    @Override
    public TblResponseEntity<PtUserEntity> queryUserInfoListByPage(SearchUserEntity entity, PagingEntity paging) {

        TblResponseEntity<PtUserEntity> responseEntity = new TblResponseEntity<>();
        Integer total = userDao.queryUserTotal(entity);
        responseEntity.setTotal(total);

        paging.deal(PtUserEntity.class,total);

        responseEntity.setPage(paging.getPage());

        List<PtUserEntity> rawRecords = userDao.queryUserInfoListByPage(entity,paging);
        responseEntity.setRawRecords(rawRecords);



        responseEntity.deal();

        return responseEntity;
    }

    @Override
    public PtUserEntity queryUserInfoById(String userUuid) {
        return userDao.queryUserInfoById(userUuid);
    }

    @Override
    public void modifyUserInfoById(PtUserEntity entity) throws EmailRepeatException, MobileRepeatException, UserNameRepeatException {
        List<PtUserEntity> entityList = userDao.queryUserInfoByEmail(entity);
        if(entityList.size() > 0) {
            throw new EmailRepeatException("email repeat ");
        }

        entityList = userDao.queryUserInfoByMobile(entity);
        if(entityList.size() > 0) {
            throw new MobileRepeatException("mobile repeat ");
        }

        entityList = userDao.queryUserInfoByUserName(entity);
        if(entityList.size() > 0) {
            throw new UserNameRepeatException();
        }

        userDao.modifyUserInfoById(entity);
    }

    @Override
    public void addUserInfo(PtUserEntity entity) throws EmailRepeatException, MobileRepeatException, UserNameRepeatException {
                List<PtUserEntity> entityList = userDao.queryUserInfoByEmail(entity);
        if(entityList.size() > 0) {
            throw new EmailRepeatException("email repeat ");
        }

        entityList = userDao.queryUserInfoByMobile(entity);
        if(entityList.size() > 0) {
            throw new MobileRepeatException("mobile repeat ");
        }

        entityList = userDao.queryUserInfoByUserName(entity);
        if(entityList.size() > 0) {
            throw new UserNameRepeatException();
        }
        String password = entity.getPassword();
        password = passwordEncoder.encode(password);
        entity.setPassword(password);

        userDao.addUserInfo(entity);
    }

    @Override
    public List<ZtreeEntity> queryRoleTreeListByUser(String userUuid) {
        List<ZtreeEntity> ztreeEntityList = userDao.queryRoleTreeList();
        List<String> roleArray = userDao.queryRoleByUser(userUuid);
        for (ZtreeEntity ztreeEntity:ztreeEntityList) {
            for (String roleUuid:roleArray) {
                if(roleUuid.equals(ztreeEntity.getId())) {
                    ztreeEntity.setChecked(true);
                }
            }
        }
        return ztreeEntityList;
    }

    @Override
    public void saveRoleRefUser(String userUuidArray, String roleUuidArray) {
        String[] userUuidArray1 = userUuidArray.split(",");
        String[] roleUuidArray1 = roleUuidArray.split(",");
        for (String userUuid:userUuidArray1) {
            userDao.deleteRoleByUser(userUuid);
            if("".equals(roleUuidArray)) {
                continue;
            }
            for (String roleUuid:roleUuidArray1) {
                userDao.insertRoleRefUser(userUuid,roleUuid);
            }
        }
    }


}
